//
//  SCNBorrowHeadTextField.swift
//  ShoucainuSwift
//
//  Created by 王家旭 on 2017/3/27.
//  Copyright © 2017年 Riluee. All rights reserved.
//

import UIKit

class SCNBorrowHeadTextField: UITextField,UITextFieldDelegate {

    var title: String?
    
    var action: Bool?
    
    
    init(frame: CGRect, title: String, placeholder: String, action: Bool, tag: Int) {
        super.init(frame: frame)
        
        self.delegate = self
        self.borderStyle = .roundedRect
        self.tag = tag
        self.placeholder = placeholder
        self.title = title
        self.action = action
        self.font = UIFont.systemFont(ofSize: 15.0)
        
        configurationUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configurationUI() {
        
        let leftView = UIView(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(80), height: CGFloat(frame.size.height)))
        
        let titleLabel = UILabel(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(75), height: CGFloat(frame.size.height)))
        titleLabel.text = title
        titleLabel.textColor = SCNConstants.colorWith255RGBA(136, g: 136, b: 136, a: 1)
        titleLabel.textAlignment = .right
        titleLabel.font = UIFont.systemFont(ofSize: CGFloat(16.0))
        leftView.addSubview(titleLabel)
        let lineView = UIView(frame: CGRect(x: CGFloat(79), y: CGFloat(8), width: CGFloat(1), height: CGFloat(18)))
        lineView.backgroundColor = SCNConstants.colorWithHexString("#CACACA")
        leftView.addSubview(lineView)
        self.leftView = leftView
        leftViewMode = .always
        
        if action!
        {
            let rightView = UIView(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(30), height: CGFloat(35)))
            let imgView = UIImageView(frame: CGRect(x: CGFloat(8), y: CGFloat(11), width: CGFloat(13), height: CGFloat(13)))
            imgView.image = UIImage(named: "icon_xiala")
            imgView.contentMode = .center
            rightView.addSubview(imgView)
            self.rightView = rightView
            rightViewMode = .always
        }
    }


    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        if action! {
            return false
        }
        return true
    }
}
